<?php
session_start();
class Admin extends CI_Controller{
    //put your code here
    
    public function __construct(){
        parent::__construct();
        $this->load->model('Login_m');
        $this->load->model('Account_m');
        $this->load->helper("url");
        $this->load->helper('form');
        $this->load->model('Download_m'); 
        $this->load->model('Upload_m'); 
        $this->load->model('Payment_m');
        $this->load->library('form_validation');
        $this->load->library('session');
        $this->load->library("my_layout"); // Sử dụng thư viện layout
        //$session_data_sm = $this->session->userdata('logged_in_sm');
	//$data['id_sm'] = $session_data_sm['id_sm'];
        $this->my_layout->setLayout("layout/admin_l"); // load file layout chính (view/layout/frontend.php)
        
    }
    // Controller mặc định cho trang chủ
    public function index(){
        $data['title'] = "ホームページ";
        $session_data_sm = $this->session->userdata('logged_in_ad');
	$data['id_ad'] = $session_data_sm['id_ad'];
        $data['data'] = "ホームページのデータ";
        $data['money_per_test']=10000;
        if($session_data_sm['id_ad']!="")
        {
            $this->my_layout->view("frontend/home",$data);
        }
        else
        {
            redirect('login','refresh');
        }
    }

    public function forum(){
        
        $data['title'] = "フォーラム";
        $session_data_sm = $this->session->userdata('logged_in_ad');
	$data['id_ad'] = $session_data_sm['id_ad'];
        if($session_data_sm['id_ad']!="")
        {
        $this->my_layout->view("frontend/forum",$data);
        }
        else
        {
            redirect('login','refresh');
        }
    }
    public function about(){
        $data['title'] = "紹介";
        $session_data_sm = $this->session->userdata('logged_in_ad');
	$data['id_ad'] = $session_data_sm['id_ad'];
        if($session_data_sm['id_ad']!="")
        {
        $this->my_layout->view("frontend/about",$data);
        }
        else
        {
            redirect('login','refresh');
        }
    }
    function download()
    {
        $data['title']="ダウンロード";
        $session_data_ad = $this->session->userdata('logged_in_ad');
        $data['id_ad'] = $session_data_ad['id_ad'];
        $data['user'] = "admin";
        $this->load->helper(array('form'));
        if($session_data_ad['id_ad']!="")
        {
        $this->my_layout->view('download/download_v',$data);
        }
        else
        {
            redirect('login','refresh');
        }
    }
    function verifydownload()
    {
        $data['title']="ダウンロード";
        $session_data_ad = $this->session->userdata('logged_in_ad');
        $data['id_ad'] = $session_data_ad['id_ad'];
        if($session_data_ad['id_ad']!="")
        {
        $filedown=$this->input->post('filedown');
        $name=$this->Download_m->getCSVfromDB($filedown);
        $data['name']=$name;
        foreach ($name as $rows) {
        ////code hieu
            $session_array_download=array('name'=>$rows->name,'link'=>$rows->link);
            $this->session->set_userdata('download_file',$session_array_download);
            //echo $rows->Name;


           //echo anchor('downfile', 'Download');
            //anchor($uri, $title, $attributes)
            //echo "<br>";         
        }
        
        $this->my_layout->view('download/downfile_v',$data);
        }
        else
        {
            redirect('login','refresh');
        }
    }
    public function downfile($id)
    {
        //$session_download = $this->session->userdata('download_file');
        //$test=$session_download['name'];
        //$test=$_GET['STT'];
        $data = file_get_contents("./csv_source/".$id); 
        force_download($id, $data);
    }
    public function add_manager(){
        $session_data_sm = $this->session->userdata('logged_in_ad');
        if($session_data_sm['id_ad']!="")
        {
	$data['id_ad'] = $session_data_sm['id_ad'];
        $data['title'] = "マネージャーを追加";
        $this->my_layout->view("add_account/add_manager_v",$data);
        }
        else
        {
            redirect('login','refresh');
        }
    }
    public function manage_manager(){
        $session_data_sm = $this->session->userdata('logged_in_ad');
        if($session_data_sm['id_ad']!="")
        {
	$data['id_ad'] = $session_data_sm['id_ad'];
        $data['title'] = "マネージャーを変更";
       // redirect('admin/search_manager','refresh');
        $data['query_list_manager']=$this->Account_m->list_users('manager');
        $this->my_layout->view("edit_account/list_manager_v",$data);
        }
        else
        {
            redirect('login','refresh');
        }
    }
    public function search_manager(){
        $session_data_sm = $this->session->userdata('logged_in_ad');
        if($session_data_sm['id_ad']!="")
        {
	$data['id_ad'] = $session_data_sm['id_ad'];
        $data['title'] = "マネージャーを変更";
        $search=  $this->input->post('timkiem'); 
        if($this->Account_m->search_database_null_manager($search,'manager')==TRUE){
                $data['query']=$this->Account_m->search_database_mg($search,'manager');
                $this->my_layout->view("edit_account/edit_manager_v",$data);
                
        }
        else{
            echo "<script type='text/javascript'>";	
	    echo "alert ('結果がありません!!! ');";
	    echo "</script>";
            redirect('admin/manage_manager','refresh');
        }
        }
        else
        {
            redirect('login','refresh');
            
        }
        
    }
     public function update_manager($id_mg){
         $session_data_sm = $this->session->userdata('logged_in_ad');
         if($session_data_sm['id_ad']!="")
         {
         $data['id_ad'] = $session_data_sm['id_ad'];
         $data['title'] = "update info";
         $query=$this->Account_m->get_manager($id_mg);
         foreach ($query as $row)
         {
            $data['id_mg']=$row->id_mg;
            $data['email_mg']=$row->email;
            $data['name_mg']=$row->name;
            $data['phone_mg']=$row->phone;
            $data['address_mg']=$row->address;
            $data['year']=$row->year;
         }
         $this->my_layout->view("edit_account/update_manager_v",$data);
         }
         else
         {
            redirect('login','refresh');
         }
     }
     public function update_manager_success(){
         $session_data_sm = $this->session->userdata('logged_in_ad');
         
         
         //$password=$this->input->post('password');
         $id_mg=$this->input->post('txtIDManager');
         $name_mg=$this->input->post('txtNameManager');
         //$id_mg=$this->input->post('id');
         $email_mg=$this->input->post('txtEmailManager');
         $address_mg=$this->input->post('txtAddressManager');
         $phone_mg=$this->input->post('txtPhoneManager');
         $year=$this->input->post('year');
         /////Check time
         $pieces = explode(" ",$year); 
         $pieces_start_string=explode("/",$pieces[0]); 
         $pieces_end_string=explode("/",$pieces[1]); 
         $pieces_start[0]=(int)$pieces_start_string[0];
         $pieces_start[1]=(int)$pieces_start_string[1];
         $pieces_start[2]=(int)$pieces_start_string[2];
         $pieces_end[0]=(int)$pieces_end_string[0];
         $pieces_end[1]=(int)$pieces_end_string[1];
         $pieces_end[2]=(int)$pieces_end_string[2];
         //echo  $pieces_start[0]."-".$pieces_start[1]."-".$pieces_start[2]."<br>";
         //echo  $pieces_end[0]."-".$pieces_end[1]."-".$pieces_end[2]."<br>";
         $result_check_date_time=$this->check_date_time($pieces_start[0],$pieces_start[1],$pieces_start[2],$pieces_end[0],$pieces_end[1],$pieces_end[2]);
         //echo $result_check_date_time;
        /////
         
         if($session_data_sm['id_ad']!="")
         {
            if ($result_check_date_time==0)
            {
                echo "<script type='text/javascript'>";	
                echo "alert ('契約の時間が間違います');";
                echo "</script>";
                redirect('admin/update_manager/'.$id_mg,'refresh'); 
            }
            else
            {
            $this->Account_m->update_manager($id_mg,$email_mg,$name_mg,$address_mg,$phone_mg,$year);
            $data['id_ad'] = $session_data_sm['id_ad'];
            $data['title'] = "update success";
            $query=$this->Account_m->get_manager($id_mg);
            foreach ($query as $row)
            {
                $data['id_mg']=$row->id_mg;
                $data['email_mg']=$row->email;
                $data['name_mg']=$row->name;
                $data['phone_mg']=$row->phone;
                $data['address_mg']=$row->address;
          
                $data['year']=$row->year;
            }
            $this->my_layout->view("manager/profile_mg_v",$data);
            // $data['query_list_manager']=$this->Account_m->list_users('manager');
            $this->my_layout->view("edit_account/update_manager_success_v",$data);
            }
         }
         else
         {
         redirect('login','refresh');
         }
      }
      public function delete_manager($id_mg)
      {
         $session_data_sm = $this->session->userdata('logged_in_ad');
         if($session_data_sm['id_ad']!="")
         {
         $data['id_ad'] = $session_data_sm['id_ad'];
         $data['title'] = "Delete success";
         $this->Account_m->delete_manager($id_mg);
         $data['query_list_manager']=$this->Account_m->list_users('manager');
         $this->my_layout->view("edit_account/update_manager_success_v",$data);
         }
         else
         {
         redirect('login','refresh');
         }
      }
    public function admin_change_pass()
    {
        $data['title'] = "パスワードの修正";
        $data['user']="admin";
        $session_data_sm = $this->session->userdata('logged_in_ad');
        if($session_data_sm['id_ad']!="")
        {
	$data['id_ad'] = $session_data_sm['id_ad'];
        $this->my_layout->view("frontend/change_pass",$data);
        }
        else
         {
         redirect('login','refresh');
         }
        
    }
    //manager_change pass
    public function success_change_pass()
    {
        $data['title'] = "パスワードの修正";
        $data['user']="admin";
        $session_data_sm = $this->session->userdata('logged_in_ad');
	$data['id_ad'] = $session_data_sm['id_ad'];
        $new_pass1=$this->input->post('new_pass');
        $new_pass2=$this->input->post('new_pass_again');
        $old_pass=md5($this->input->post('old_pass'));
        $query= $this->Account_m->search_admin_pass($data['id_ad']);
        
        foreach ($query as $row)
         {
            $data['password']=$row->password;        
         }
        if($data['password']!=$old_pass)
         {
             $data['error1']="古いパスワードが違う";
             $this->my_layout->view("frontend/change_pass",$data);
         }
        else if($new_pass1!=$new_pass2)
        {
        $data['error']="新しいパスワードと新しいパスワードの重複が違う";
        $this->my_layout->view("frontend/change_pass",$data);
        }
        else
        {
            
            $this->Account_m->update_admin_pass($data['id_ad'],$new_pass1);
            echo "<script type='text/javascript'>";	
	    echo "alert ('パスワードを変更しました!!! ');";
	    echo "</script>";
            if($session_data_sm['id_ad']!="")
            {
             $this->my_layout->view("frontend/home",$data);
            }
            else
            {
            redirect('login','refresh');
            }
        }
        
    }
    ///////Code create Payment
    public function create_payment()
        {
            $data['title'] = "契約作成";
            $data['user']="admin";
            //$id_manager=$this->Payment_m->get_id_manager();
            $session_data_sm = $this->session->userdata('logged_in_ad');
            $data['id_ad'] = $session_data_sm['id_ad'];
            //print_r($id_manager);
            $data['query']=$this->Payment_m->get_id_manager();
            $this->my_layout->view('payment/payment_id_v',$data); 
        }
        public function payment_list_time()
        {
            $data['title'] = "契約作成";
            $data['user']="admin";
            $session_data_sm = $this->session->userdata('logged_in_ad'); 
            $data['id_ad'] = $session_data_sm['id_ad'];
            $id_choice=$this->input->post('id_mg');
            $sess_array_manager1 = array('id_mg_choice' => $id_choice);
            $this->session->set_userdata('logged_in_mg_choice', $sess_array_manager1);
            ///lay time tu he thong
            //$date = gmdate("Y-m-d-H-i-s", time()+7*3600);
            $today = getdate();
            $show_date_test_by_id_manager=$this->Payment_m->show_date_test_by_id_manager($id_choice);
            /// Tinh so lan thi trong thang nay
            $count_submit_this_month=0;
            foreach ($show_date_test_by_id_manager->result() as $row)
            {
                //$count_show_date_test_by_id_manager++;
                $time= $row->date_test;
                $pieces_time= explode("-", $time);
                $pieces_time_year = $pieces_time[0];
                $pieces_time_month = $pieces_time[1];
                if (($pieces_time_year==$today['year'])&&($pieces_time_month==$today['mon']))  $count_submit_this_month++;
            }
            ////
            $this_month=$today['year']."-".$today['mon'];
            $money_per_test=$this->Payment_m->get_money_per_test($id_choice);
            if ($money_per_test==NULL) 
            {
                $this->Payment_m->insert_new_money_per_test($id_choice,10000);
                $get_money_per_test=10000;  
            }
            else $get_money_per_test=$money_per_test[0]->new_money_per_test;
            //echo $get_money_per_test;

            $total_money= $get_money_per_test*$count_submit_this_month;
            
            ////Lay DB tu manager
            $result=$this->Payment_m->get_payment_info($id_choice);
            $address = $result[0]->address;
            $phone = $result[0]->phone;
            $check=$this->Payment_m->check_exist_payment_on_list($id_choice,$this_month);
            if ($check==NULL) 
            {
                $this->Payment_m->create_payment_this_month($id_choice,$this_month,$count_submit_this_month,$get_money_per_test,$total_money,$address,$phone);
            }
            else
            {
                $this->Payment_m->update_db_to_list_payment($id_choice,$this_month,$count_submit_this_month,$get_money_per_test,$total_money);
            }
            $data['query']=$this->Payment_m->show_payment_by_id_mg($id_choice);
            $this->my_layout->view('payment/admin_view_payment_on_month_v',$data); 
 
        }
        public function create_payment_result()
        {
            $data['title'] = "契約作成";
            $data['user']="admin";
            $session_data_sm = $this->session->userdata('logged_in_ad'); 
            $data['id_ad'] = $session_data_sm['id_ad'];
            $session_data_sm1 = $this->session->userdata('logged_in_mg_choice');
            $data['id_mg_choice'] = $session_data_sm1['id_mg_choice'];
            $id_choice=$session_data_sm1['id_mg_choice'];
            $time_choice=$this->input->post('time');
            $create_CSV=$this->Payment_m->create_CSV_payment_by_month($id_choice,$time_choice);
            $time_pieces = explode("-", $time_choice);
            
            //print_r($create_CSV);
             //ghi file
                $path="./csv_source/".$id_choice.".".$time_pieces[0].".".$time_pieces[1].".csv";
                $file=fopen($path, "w");
                fwrite($file,"契約書: \n団体: ".$create_CSV[0]->id_mg."\n時間:　".$time_pieces[0]."年".$time_pieces[1]."月\nアドレス: ".$create_CSV[0]->address."\n電話番号: ".$create_CSV[0]->phone."\nサブミットの代金: ".$create_CSV[0]->money_per_test."VND\nサブミットしたテスト: ".$create_CSV[0]->total_submit_this_month."\n合計代金: ".$create_CSV[0]->total_money."VND");
                fclose($file);
                //
            $data['query']=$this->Payment_m->create_CSV_payment_by_month($id_choice,$time_choice);
            $data['message']="この情報は  ".$id_choice.".".$time_pieces[0].".".$time_pieces[1].".csv"."  ファイルに書きました。";
            $this->my_layout->view('payment/success_create_payment_v',$data); 
        }
////Code Fix payment
        
        public function change_payment()
        {
            $data['title'] = "契約変更";
            $data['user']="admin";
            $id_manager=$this->Payment_m->get_id_manager();
            $session_data_sm = $this->session->userdata('logged_in_ad');
            $data['id_ad'] = $session_data_sm['id_ad'];
            $data['query']=$this->Payment_m->get_id_manager();
            $this->my_layout->view('payment/change_payment_id_v',$data); 
        }
        public function payment_view()
	{
            $data['title'] = "契約変更";
            $data['user']="admin";
            $session_data_sm = $this->session->userdata('logged_in_ad'); 
            $data['id_ad'] = $session_data_sm['id_ad'];
            $id_choice=$this->input->post('id_mg');
            $sess_array_manager1 = array('id_mg_choice' => $id_choice);
            $this->session->set_userdata('logged_in_mg_choice', $sess_array_manager1);
            $check=$this->Payment_m->check_payment_of_new_mg($id_choice);
            if ($check==NULL) 
            {
                echo "<script type='text/javascript'>";	
                echo "alert ('この団体は契約の情報はまだありませんので、まず契約作成の機能をしてお願いします!!! ');";
                echo "</script>";
                redirect('admin/change_payment','refresh');
            }
            else $this->my_layout->view('payment/change_payment_v',$data);  
            //
        }
        function change_payment_money()
        {
            $data['title'] = "契約変更";
            $data['user']="admin";
            $session_data_sm = $this->session->userdata('logged_in_ad');
            $data['id_ad'] = $session_data_sm['id_ad'];
            $session_data_sm1 = $this->session->userdata('logged_in_mg_choice');
            $data['id_mg_choice'] = $session_data_sm1['id_mg_choice'];
            $id_choice=$session_data_sm1['id_mg_choice'];
            //echo $id_mg_choice;
            $new_ques_money=$this->input->post('new_ques_money');
            $this->Payment_m->update_payment_ques_money($id_choice,$new_ques_money);
            $data['message']="これから、団体　".$id_choice."　のサブミットの代金は　".$new_ques_money." VND になります!!!";
            $this->my_layout->view('payment/success_change_money_per_test_v',$data); 
        }
      
 ///////////////     
    public function logout()
    {
        $session_data_ad = $this->session->userdata('logged_in_ad');
        $id_ad = $session_data_ad['id_ad'];
        $this->Login_m->update_online_ad($id_ad,0);
        $this->session->unset_userdata('logged_in_ad');
        //session_destroy();
        redirect('login','refresh');
    }
    //public function check_date_time($pieces_start[0],$pieces_start[1],$pieces_start[2],$pieces_end[0],$pieces_end[1],$pieces_end[2])
    //public function check_date_time($pieces_start[0],$pieces_start[1],$pieces_start[2],$pieces_end[0],$pieces_end[1],$pieces_end[2])
    public function check_date_time($pieces1,$pieces2,$pieces3,$pieces4,$pieces5,$pieces6)
    {
        $result_check=1;
        if (($pieces1<=0)||($pieces2<=0)||($pieces3<=0)||($pieces4<=0)||($pieces5<=0)||($pieces6<=0)) $result_check=0;
        else
        {
            if (($pieces2>=13)||($pieces5>=13)) $result_check=0;
            if (($pieces3>=32)||($pieces6>=32)) $result_check=0;
            if ($pieces1>$pieces4) $result_check=0;
            else
            {
                if($pieces1==$pieces4)
                {
                    if ($pieces2>$pieces5) $result_check=0;
                    else
                    {
                        if(($pieces2==$pieces5))
                        {
                            if($pieces3>$pieces6) $result_check=0;
                        }
                    }
                }
                
            }
        }
        return $result_check;
    }
    
    
}
?>
